package br.com.windnfe.persistence;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Disjunction;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;

import br.com.caelum.vraptor.ioc.Component;
import br.com.windnfe.ipersistence.IPaisDAO;
import br.com.windnfe.model.Pais;

@Component
public class PaisDAO implements IPaisDAO {
	Session session = null;
	Criteria criteria = null;

	public PaisDAO() {
		session = ConectionFactory.getSession();
		criteria = session.createCriteria(Pais.class, "p");
		
	}

	public void add(Pais pais) {
		session.beginTransaction();
		session.persist(pais);
		session.getTransaction().commit();
	}

	public void remove(Pais pais) {
		// TODO Auto-generated method stub
		
	}

	public List findAll() {
		return this.criteria.list();
	}
	public List findWithTerm(String term) {
		Disjunction ors = Restrictions.disjunction();
		ors.add(Restrictions.ilike("p.ibge", term,MatchMode.ANYWHERE));
		ors.add(Restrictions.ilike("p.nome", term,MatchMode.ANYWHERE));
		ors.add(Restrictions.ilike("p.sigla", term,MatchMode.ANYWHERE));
		this.criteria.add(ors);
		return this.criteria.list();
	}
}
